package com.lanou.timeline.service.impl;

import com.github.pagehelper.PageHelper;
import com.lanou.timeline.entity.Timeline;
import com.lanou.timeline.entity.Userinfo;
import com.lanou.timeline.mapper.TimelineMapper;
import com.lanou.timeline.web.controller.Atest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class TimelineServiceImpl {

    @Autowired
    private TimelineMapper timelineMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    Atest atest;


    public List<Timeline> showByTagId(Long tagId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return timelineMapper.showByTagId(tagId);
    }

    public List<Timeline> showByTagIdAndUid(Long tagId,Integer uid,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return timelineMapper.showByTagIdAndUid(tagId,uid);
    }

    public boolean showIsLike(String timelineId, Integer uid) {
        int result = timelineMapper.showIsLike(timelineId,uid);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    public void updateIslike(String timelineId, Integer uid) {
        if (timelineMapper.showMyTimeline(timelineId, uid) == 0) {
            if (showIsLike(timelineId, uid) == false) {
                timelineMapper.addIslike(timelineId, uid);
            } else
                timelineMapper.delIslike(timelineId, uid);
        }
    }

    public void reoprtTimeline(String timelineId,Integer uid) {
        Integer reportCount = timelineMapper.selectTimelineReport(timelineId)+1;
        timelineMapper.reoprtTimeline(timelineId,uid,reportCount);
    }

    public void deleteTimeline(String timelineId, Integer uid) {
        timelineMapper.deleteTimeline(timelineId,uid);
    }


    public List<Timeline> selectByUid(Integer uid) {
        return timelineMapper.selectByUid(uid);
    }

    public Timeline selectByTimelineId(String timelineId) {
        return timelineMapper.selectByTimelineId(timelineId);
    }

    public void releaseTimeline(Timeline timeline) {
        long addtime = (new Date().getTime()) / 1000;
        timeline.setSecret(0);
        timeline.setAddtime(addtime);
        String s = Integer.toHexString((int) addtime);
        String s1 = Integer.toHexString(timeline.getUid());
        timeline.setTimelineId(s+s1);
        timelineMapper.releaseTimeline(timeline);
    }

    public List<Timeline> relevantTimeline() {
        return timelineMapper.relevantTimeline();
    }

    public List<Timeline> showAllTimeline() {
        return timelineMapper.showAllTimeline();
    }

    public List<Userinfo> selectUserActive() {
        long addtime = (new Date().getTime())/1000 - 2590000;
        return timelineMapper.selectUserActive(addtime);
    }

    public void releaseTimelineSecret(Timeline timeline) {
        long addtime = (new Date().getTime())/1000;
        String s = Integer.toHexString((int) addtime);
        String s1 = Integer.toHexString(timeline.getUid());
        timeline.setTimelineId(s+s1);
        timeline.setSecret(1);
        timeline.setAddtime(addtime);
        Set userinfos = redisTemplate.opsForZSet().range("userActive", 0, -1);
        List userlist = new ArrayList();
        List fiveUser = new ArrayList<>();
        for (Object userinfo : userinfos) {
            userlist.add( userinfo);
        }
        for (int i = 0; i < 5; i++) {
            int index = (int) (Math.random()*userlist.size());
            Object userinfo = userlist.get(index);
            int uid = Integer.parseInt(String.valueOf(userinfo));
            fiveUser.add(uid);
            userlist.remove(index);
            timelineMapper.addIsSecret(uid,timeline.getTimelineId());
        }
        timelineMapper.insertTimelineSecret(timeline);



    }
}
